package com.manage.controller;

import com.manage.controller.request.RepairRequest;
import com.manage.controller.request.RepairTaskSaveRequest;
import com.manage.controller.response.RepairTaskListResponse;
import com.manage.controller.request.RepairTaskListRequest;
import com.manage.pojo.entity.RepairMaterial;
import com.manage.pojo.entity.RepairTask;
import com.manage.service.RepairMaterialService;
import com.manage.service.RepairTaskService;
import com.manage.util.ObjectUtil;
import com.manage.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/repair_task")
public class RepairTaskController {

    @Autowired
    RepairTaskService repairTaskService;

    @Autowired
    RepairMaterialService repairMaterialService;

    @PostMapping("/list")
    public List<RepairTaskListResponse> listRepairTask(@RequestBody RepairTaskListRequest request) {
        return repairTaskService.listTask(request);
    }

    @PostMapping("/listNeedRepair")
    public List<RepairTaskListResponse> listNeedRepair(@RequestBody RepairTaskListRequest request) {
        return repairTaskService.listNeedRepair(request);
    }

    @PostMapping("/listNeedCheck")
    public List<RepairTaskListResponse> listNeedCheck(@RequestBody RepairTaskListRequest request) {
        return repairTaskService.listNeedCheck(request);
    }

    @PostMapping("/listMoney")
    public List<RepairTaskListResponse> listMoney(@RequestBody RepairTaskListRequest request) {
        return repairTaskService.listMoney(request);
    }

    @PostMapping("/repair")
    public Result repair(@RequestBody RepairRequest request) {
        if (request.getMaterialId() != null) {
            RepairMaterial material = repairMaterialService.getById(request.getMaterialId());
            if (request.getNum() == null) {
                request.setNum(1);
            }
            if (material.getNum() < request.getNum()) {
                return Result.error("部件数量不够");
            }
            repairTaskService.repair(request);
        }
        RepairTask task = repairTaskService.getById(request.getTaskId());
        task.setStatus(2);
        task.setRepairTime(new Date());
        repairTaskService.updateById(task);
        return Result.success("维修成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody RepairTaskSaveRequest request) {
        RepairTask repairTask = ObjectUtil.clone(request, RepairTask.class);
        repairTaskService.save(repairTask);
        return Result.success("添加成功");
    }

    @PostMapping("/update")
    public Result update(@RequestBody RepairTaskSaveRequest request) {
        RepairTask repairTask = ObjectUtil.clone(request, RepairTask.class);
        repairTaskService.updateById(repairTask);
        return Result.success("修改成功");
    }

    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {
        repairTaskService.removeById(id);
        return Result.success("删除成功");
    }

    @GetMapping("/check/{id}")
    public Result check(@PathVariable("id") Long id) {
        RepairTask task = repairTaskService.getById(id);
        task.setCheckTime(new Date());
        task.setStatus(1);
        repairTaskService.updateById(task);
        return Result.success("质检完成");
    }
}
